<?php namespace App\Http\Controllers\user\uc\third;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use Session;
use Validator;
use App;
use App\Models\ThirdpartyProfessionalList;
use App\Models\ThirdpartyIndustry;
use App\Models\ThirdpartyDevice;
use App\Models\ThirdpartyHistoryProject;

class IndustryInfoController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$uid = $this->getUid();
		$thirdParty = ThirdpartyIndustry::where('uid', $uid);
		if($thirdParty->count()){
			$indid = $thirdParty->get()[0]->id;
		}else{
			return App::abort(404);
		}
		$profession = ThirdpartyProfessionalList::where("industry_id", $indid)->get();
		$device = ThirdpartyDevice::where("industry_id", $indid)->get();
		$history = ThirdpartyHistoryProject::where("industry_id", $indid)->get();
		return view('user.uc.third.industryinfo')->with(['profession' => $profession])->with(['device' => $device])->with(['history' => $history])->withIsThirdIndustry(true);
	}

	public function editArtisan()
	{
		return view('user.uc.third.edit_artisan')->withIsThirdIndustry(true);
	}

	public function editInstrument()
	{
		return view('user.uc.third.edit_instrument')->withIsThirdIndustry(true);
	}

	public function editProjects()
	{
		return view('user.uc.third.edit_projects')->withIsThirdIndustry(true);
	}

	public function saveArtisan()
	{
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		$indid = $industry->get()[0]->id;
		$data = Input::all();
		$data['industry_id'] = $indid;
		if(ThirdpartyProfessionalList::create($data)){
			return $this->json([]);
		}

	}

	public function getArtisan()
	{
		$pid = Input::get("pid");
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$industry_id = $industry->get()[0]->id;
			$model = ThirdpartyProfessionalList::where("id", $pid)->where("industry_id", $industry_id);
			if($model->count())
			{
				$data = $model->get()[0]->toArray();
				return $this->json($data);
			}
			else
			{
				return;
			}
		}
	}

	public function updateArtisan()
	{
		$data = Input::except("pid");
		$pid = Input::get("pid");
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$industry_id = $industry->get()[0]->id;
			$model = ThirdpartyProfessionalList::where("id", $pid)->where("industry_id", $industry_id);
			if($model->count())
			{
				$model->update($data);
				return $this->json([]);
			}
			else
			{
				return;
			}
		}
		else
		{
			return;
		}
	}


	public function deleteArtisan()
	{
		$pid = Input::get("pid");
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$industry_id = $industry->get()[0]->id;
			$model = ThirdpartyProfessionalList::where("id", $pid)->where("industry_id", $industry_id);
			if($model->count())
			{
				$model->delete();
				return $this->json([]);
			}
			else
			{
				return;
			}
		}
		else
		{
			return;
		}
	}


	public function addInstrument()
	{
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$indid = $industry->get()[0]->id;
			$data = Input::all();
			$data['industry_id'] = $indid;
			ThirdpartyDevice::create($data);
			return $this->json([]);
		}
		else
		{
			return;
		}
	}


	public function getInstrument()
	{
		$tid = Input::get("tid");
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$industry_id = $industry->get()[0]->id;
			$model = ThirdpartyDevice::where("id", $tid)->where("industry_id", $industry_id);
			if($model->count())
			{
				$data = $model->get()[0]->toArray();
				return $this->json($data);
			}
			else
			{
				return;
			}
		}
	}

	public function updateInstrument()
	{
		$data = Input::except("tid");
		$tid = Input::get("tid");
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$industry_id = $industry->get()[0]->id;
			$model = ThirdpartyDevice::where("id", $tid)->where("industry_id", $industry_id);
			if($model->count())
			{
				$model->update($data);
				return $this->json([]);
			}
			else
			{
				return;
			}
		}
		else
		{
			return;
		}
	}

	public function deleteInstrument()
	{
		$tid = Input::get("tid");
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$industry_id = $industry->get()[0]->id;
			$model = ThirdpartyDevice::where("id", $tid)->where("industry_id", $industry_id);
			if($model->count())
			{
				$model->delete();
				return $this->json([]);
			}
			else
			{
				return;
			}
		}
		else
		{
			return;
		}
	}

	public function addProjects()
	{
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$indid = $industry->get()[0]->id;
			$data = Input::all();
			$data['industry_id'] = $indid;
			ThirdpartyHistoryProject::create($data);
			return $this->json([]);
		}
		else
		{
			return;
		}
	}


	public function getProjects()
	{
		$hid = Input::get("hid");
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$industry_id = $industry->get()[0]->id;
			$model = ThirdpartyHistoryProject::where("id", $hid)->where("industry_id", $industry_id);
			if($model->count())
			{
				$data = $model->get()[0]->toArray();
				return $this->json($data);
			}
			else
			{
				return;
			}
		}
	}


	public function updateProjects()
	{
		$hid = Input::get("hid");
		$data = Input::except("hid");
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$industry_id = $industry->get()[0]->id;
			$model = ThirdpartyHistoryProject::where("id", $hid)->where("industry_id", $industry_id);
			if($model->count())
			{
				$model->update($data);
				return $this->json([]);
			}
			else
			{
				return;
			}
		}
		else
		{
			return;
		}
	}

	public function deleteProjects()
	{
		$hid = Input::get("hid");
		$uid = $this->getUid();
		$industry = ThirdpartyIndustry::where("uid", $uid);
		if($industry->count())
		{
			$industry_id = $industry->get()[0]->id;
			$model = ThirdpartyHistoryProject::where("id", $hid)->where("industry_id", $industry_id);
			if($model->count())
			{
				$model->delete();
				return $this->json([]);
			}
			else
			{
				return;
			}
		}
		else
		{
			return;
		}
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
